<?php
namespace Home\Controller;
use Think\Controller;
class CollectController extends ZMController {
	public function hcustcollect(){//收集
		$hid=I('custid');
		$pdate=I('date');
		$Model=M();
		if(!empty($hid)&&!empty($pdate)){
		    header("Content-type: text/html; charset=utf-8");
		    $res=$Model->db(1,'DB_CONFIG1')->query("select a.cid,a.ctime,a.num_0,a.num_1,a.num_3,a.num_4,a.num_5,a.num_6,nval1_2,nval1_5,b.name as custname,b.areaid,c.name as cname,d.name as sname from ddwk_collect as a left join ddwk_hcustomer as b on a.custid=b.id left join ddwk_employee as c on a.empid=c.id left join ddwk_employee as d on a.sid=d.id left join ddwk_hospital as e on a.hid=e.hid where a.hid=$hid and a.pdate='$pdate' order by b.id asc");
		    for($i=0;$i<count($res);$i++){
				$cid=$res[$i]['cid'];
			    $arr=explode(',',$res[$i]['areaid']);
			    for($j=0;$j<count($arr);$j++){
			        $r=$Model->db(1,'DB_CONFIG1')->query("select areaname from ddwk_area where id='$arr[$j]'");
				    if($j<count($arr)-1){
				        $res[$i]['desc']=$res[$i]['desc'].$r['0']['areaname'].',';
				    }
				    else{
					    $res[$i]['desc']=$res[$i]['desc'].$r['0']['areaname'];
				    }
			    }
				$res[$i]['num_0']=$res[$i]['num_0']+$res[$i]['num_6'];
				$res[$i]['hx']=0;
				$res[$i]['wei0']=(double)$this->totalCollectweight($cid,'0');
				$res[$i]['wei1']=(double)$this->totalCollectweight($cid,'1')+(double)$this->totalCollectweight($cid,'2');
				$res[$i]['wei3']=(double)$this->totalCollectweight($cid,'3');
				$res[$i]['wei4']=(double)$this->totalCollectweight($cid,'4');
				$res[$i]['wei5']=(double)$this->totalCollectweight($cid,'5');
				
		    }
			$this->ajax($res);
		}
	}
	public function unload(){//入库
        $Model=M();
		$hid=I('custid');
		$pdate=I('date');
		if(empty($hid)||empty($pdate)){
			exit(0);
		}
		$res=$Model->db(1,'DB_CONFIG1')->query("select a.uid,a.num_0,a.fval1,a.ctime,b.name as sname,c.name as cname from ddwk_unload as a left join ddwk_employee as b on a.sid=b.id left join ddwk_employee as c on a.empid=c.id where a.hid=$hid and a.pdate='$pdate'");
		$this->ajax($res);
	}
	public function compareyy(){//收集入库转运对比
		$startdate=I('startdate');
		$enddate=I('enddate');
		$hid=I('custid');
		if(empty($hid)||empty($startdate)||empty($enddate)){
			exit(0);
		}
		$pdate=$startdate;
		$arr=array();
		while($pdate<=$enddate){
			$collectnum=$this->collectnum($pdate,$hid);//收集
			$unloadnum=$this->unloadnum($pdate,$hid);//入库
			$transfer=$this->transfer($pdate,$hid);//转运
			$res['pdate']=$pdate;
			$res['collectnum']=$collectnum['num'];
			$res['unloadnum']=$unloadnum['num'];
			$res['ctotal']=$transfer['num'];
			$res['collectweight']=$collectnum['weight'];
			$res['unloadweight']=$unloadnum['weight'];
			$res['weight']=$transfer['weight'];
			$arr[]=$res;
			$pdate=date('Y-m-d',strtotime("+1 day",strtotime($pdate)));	
		}
		$this->ajax($arr);
	}
	private function collectnum($pdate,$hid){//收集
		$Model=M('Collect');
		if(empty($hid)||empty($pdate)){
			exit(0);
		}
		$res=$Model->db(1,'DB_CONFIG1')->query("select cid,num_0,num_1,num_3,num_4,num_5 from ddwk_collect where pdate='$pdate' and hid=$hid");
		    $num=0;
		    $cid='';
		    $arr=array();
		$numt=$this->delrepeat($pdate,$hid);//计算收集重复数字
		foreach($res as $row){
			$num+=$row['num_0']+$row['num_1']+$row['num_3']+$row['num_4']+$row['num_5'];
			$cid.=','.$row['cid'];
		}
		$cid=ltrim($cid,',');
		$num=$num-$numt;//去除重复
		$weight=$this->totalCollectweight($cid);
		$arr['num']=$num;
		$arr['weight']=$weight;
		return $arr;
	}
	private function unloadnum($pdate,$hid){//入库
		$Model=M('Unload');
		if(empty($hid)||empty($pdate)){
			exit(0);
		}
		$res=$Model->db(1,'DB_CONFIG1')->query("select sum(num_0+num_1+num_4+num_5) as num,sum(fval1) as fval1 from ddwk_unload where pdate='$pdate' and hid=$hid");
		$arr=array();
		$numt=$this->delunlorepeat($pdate,$hid);//计算卸箱重复数字
		$num=$res['0']['num']-$numt;//去除重复
		$weight=$res['0']['fval1'];
		$arr['num']=$num;
		$arr['weight']=$weight;
		return $arr;
	}
	private function transfer($pdate,$hid){//转运
		$Model=M('Collect')->db(2,'DB_CONFIG2');
		if(empty($hid)||empty($pdate)){
			exit(0);
		}
		$res=$Model->query("select sum(garbage) as garbage,sum(lwq) as lwq,sum(guil) as guil,sum(qt) as qt,sum(hx) as hx,kg from ddwk_collect where pdate='$pdate' and custid=$hid group by kg");
		$arr=array();
		$num=0;
		$weight=0;
		foreach($res as $row){
			$num+=$row['garbage']+$row['lwq']+$row['guil']+$row['qt']+$row['hx'];
			$kg=$row['kg'];
			$kg=explode(',',$kg);
			$weight+=$kg['0']*$row['garbage']/1000;//感染性重量
			$weight+=$kg['1']*$row['lwq']/1000;//损伤性重量
			$weight+=$kg['2']*$row['guil']/1000;//病理性重量
			$weight+=$kg['3']*$row['qt']/1000;//药物性重量
			$weight+=$kg['4']*$row['hx']/1000;//化学性重量
		}
		$arr['num']=$num;
		$arr['weight']=$weight;
		return $arr;
	}
	private function delrepeat($pdate,$hid){//计算收集重复数字
		$Model=M();
		$rescollect = $Model->db(1,'DB_CONFIG1')->query("select cid from ddwk_collect where hid=$hid and pdate='$pdate'");
		$ccid='';
		foreach($rescollect as $row){
			$ccid.=$row['cid'].',';
		}
		$ccid=rtrim($ccid,',');
		if(!empty($ccid)){
            $rescollectitem = $Model->query("select count(*) as count from ddwk_collect_item where sno <> '000000000000' and sno<>'100000000000' and cid in ($ccid) group by sno having count(sno) > 1");
		}
		$totalrecount=0;
		if (!empty($rescollectitem)) {
            foreach ($rescollectitem as $row) {
				$recount = $row['count'] - 1;
				$totalrecount = $totalrecount + $recount;
			}
		}
		return $totalrecount;
	}
	private function delunlorepeat($pdate,$hid){//计算卸箱重复数字
		$Model=M();
		$res=$Model->db(1,'DB_CONFIG1')->query("select b.sno from ddwk_unload as a left join ddwk_unload_item as b on a.uid=b.uid where a.pdate='$pdate' and a.hid=$hid and b.sno<>'000000000000' and b.sno<>'100000000000'");
		$count1=count($res);
		$b=array();
        foreach($res as $row){
			array_push($b,$row['sno']);
		}
		$b=array_unique($b);
		$count2=count($b);
		$num=$count1-$count2;
		return $num;
	}
	private function totalCollectweight($cid,$type=NULL){//计算收集重量
		try{
			if(empty($cid)){
				E('无收集记录');
			}
			else{
				$Model=M();
				if(isset($type)){
					$where=" and type=$type";
				}
				$res=$Model->db(1,'DB_CONFIG1')->query("select sum(fval1) as weight from ddwk_collect_item where cid in ($cid) $where");
				if(!empty($res)){
				    return $res['0']['weight'];
				}
				else{
					E('数据出错');
				}
			}
		}
		catch(\Exception $e){
			$message=$e->getMessage();
			return 0;
		}
	}
}